package RobotPlayer
{
	import adobe.utils.CustomActions;
	import BasicEngine.Dispatcher;
	import BasicEngine.Vector2D;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import GameLogic.Game;
	public class BaseRobot extends MoveEntity
	{
		public var Canvas:Sprite;
		public var Offset:Point;
		public var Scale:Number;
		
     	/**
		 * 模块索引
		 */		
		public var Modules:Dictionary;
		
		public var ScanBeam:Sprite;
		
		/**
		 * 合力
		 */
		private var resultant:Resultant;
		
		private var target:Vector2D = new Vector2D(300, 300);
		
	
		
		private var shape:Shape;
		private var shape2:Shape;
		public var foot:int = 0;
		
		public var radarVector:Vector2D =  new Vector2D(1, 1);
		
		/**
		 * _velocity:Vector2D 初始速度 _mass:Number 质量 _maxSpeed:Number 最大速度 _maxForce:Number 最大受力 _scale:Number 缩放比（默认为1）
		 */
		public function BaseRobot (_velocity:Vector2D,
								   _mass:Number,
								   _maxSpeed:Number,
								   _maxForce:Number,
								   _scale:Number = 1.0
								)
		{
			super(_velocity, _mass, _maxSpeed, _maxForce);
			Canvas = new Sprite();
			Scale = _scale;
			Modules = new Dictionary();
			resultant = new Resultant(this);
			Canvas.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				Update();
			});
			shape = new Shape();
			shape.graphics.beginFill(0xff0000, 1);
			shape.graphics.drawCircle(0, 0, 5);
			Game.Building.addChild(shape);
			shape.x = target.X;
			shape.y = target.Y;
			
			
			radarVector.Normalize();
			radarVector.Length = 200;
			radarVector.Angle = Velocity.Angle;
			shape2 = new Shape();
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 0);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 2);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 4);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 6);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 8);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 10);
			shape2.graphics.lineStyle(1, 0x0000ff,.5);
            shape2.graphics.moveTo(0, 0);
            shape2.graphics.lineTo(500, 52);
			Canvas.addChild(shape2);
		}
		
		/**
		 * 装配模块
		 */
		public function AssemblyModule(_module:RobotModule,_x:int = 0, _y:int = 0):void
		{
			Modules[_module.Name] = _module;
			Canvas.addChild(_module.Canvas);
			_module.Canvas.x = _x;
			_module.Canvas.y = _y;
		}
		
		private function Update():void
		{
			if (Game.Pause)
			   return;
			var force:Vector2D = resultant.Seek(target); 	
			var acceleration:Vector2D = force.CrossProduct(1.0/Mass);
			var t1:Vector2D = acceleration.CrossProduct(Dispatcher.Clock.Tick);
			Velocity =  Velocity.Addition(t1); //更新速度 
			
			if (Math.abs(target.X - Canvas.x) < 20 && Math.abs(target.Y - Canvas.y) <20)
			{
				target = new Vector2D(Math.random() * 700, Math.random() * 500);
				shape.x = target.X;
			    shape.y = target.Y;
			}
			
			var t2:Vector2D = Velocity.CrossProduct(Dispatcher.Clock.Tick);
			t2.Turncate(MaxSpeed);
			
			Pos = Pos.Addition(t2);	
			
			Canvas.x = Pos.X;
			Canvas.y = Pos.Y;
			Modules["BottomModule"].Heading.Angle = Velocity.Normalize().Angle + Math.PI / 2;	
			Modules["TopModule"].Heading.Angle = Velocity.Normalize().Angle - Math.PI / 2;	
					
			if (Velocity.LengthSQ > 0.000001)
			{
				Heading = Velocity.NormalizeClone();
				Side = Heading.Perp;
			}
			
			radarVector.Angle += .1;
			shape2.rotation = radarVector.Angle * 180 / Math.PI;
		}
	}
}